/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Configuration.Provider;
using System.Configuration;
using DaveSexton.DocProject.Engine;
using System.Collections.Generic;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject.Configuration
{
	public sealed class BuildEngineProviderCollection : ProviderCollection, ICollection<BuildEngineProvider>
	{
		private bool readOnly;

		internal BuildEngineProviderCollection()
		{
		}

		public override void Add(ProviderBase provider)
		{
			if (provider == null)
				throw new ArgumentNullException("provider");

			BuildEngineProvider buildEngineProvider = provider as BuildEngineProvider;

			if (buildEngineProvider == null)
				throw new ArgumentException(Errors.InvalidProviderType);

			base.Add(provider);
		}

		public new BuildEngineProvider this[string name]
		{
			get { return (BuildEngineProvider) base[name]; }
		}

		public void CopyTo(BuildEngineProvider[] array, int index)
		{
			base.CopyTo(array, index);
		}

		public new void SetReadOnly()
		{
			readOnly = true;
			base.SetReadOnly();
		}

		public int IndexOf(BuildEngineProvider provider)
		{
			if (provider == null)
				throw new ArgumentNullException("provider");

			int i = -1;

			foreach (BuildEngineProvider p in this)
			{
				i++;

				if (p == provider)
					break;
			}

			return i;
		}

		#region ICollection<BuildEngineProvider> Members
		void ICollection<BuildEngineProvider>.Add(BuildEngineProvider item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			Add(item);
		}

		void ICollection<BuildEngineProvider>.Clear()
		{
			Clear();
		}

		bool ICollection<BuildEngineProvider>.Contains(BuildEngineProvider item)
		{
			foreach (BuildEngineProvider provider in this)
				if (provider == item)
					return true;

			return false;
		}

		void ICollection<BuildEngineProvider>.CopyTo(BuildEngineProvider[] array, int arrayIndex)
		{
			CopyTo(array, arrayIndex);
		}

		int ICollection<BuildEngineProvider>.Count
		{
			get
			{
				return Count;
			}
		}

		bool ICollection<BuildEngineProvider>.IsReadOnly
		{
			get
			{
				return readOnly;
			}
		}

		bool ICollection<BuildEngineProvider>.Remove(BuildEngineProvider item)
		{
			if (item == null)
				return false;

			if (this[item.Name] == null)
				return false;

			Remove(item.Name);

			return true;
		}
		#endregion

		#region IEnumerable<BuildEngineProvider> Members
		IEnumerator<BuildEngineProvider> IEnumerable<BuildEngineProvider>.GetEnumerator()
		{
			foreach (BuildEngineProvider provider in this)
				yield return provider;
		}
		#endregion

		#region IEnumerable Members
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}
		#endregion
	}
}
